package com.woniu.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.entity.Vedio;
import com.woniu.entity.dto.SaveExamRequest;
import com.woniu.service.VedioService;
import com.woniu.utils.ResultObj;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * (Vedio)表控制层
 *
 * @author makejava
 * @since 2025-06-10 11:59:04
 */
@RestController
@RequestMapping("api/vedio")
@CrossOrigin
public class VedioController {
    /**
     * 服务对象
     */
    @Autowired
    private VedioService vedioService;

    /**
     * 通过课程id获取对应课程下的视频信息列表
     * @param courseId
     * @return
     */
    @GetMapping("getVedioList/{courseId}")
    public ResultObj getVedioList(@PathVariable Integer courseId){
        List<Vedio> vedioList = vedioService.getVedioList(courseId);
        return ResultObj.ok(vedioList);
    }

    /**
     * 查询单个视频
     * @param id
     * @return
     */
    @GetMapping("/selVedioById/{id}")
    public ResultObj selVedioById(@PathVariable Integer id){
        Vedio vedio = vedioService.selVedioById(id);
        return ResultObj.ok(vedio);
    }


    //修改视频信息
    @PostMapping("/saveExam")
    public ResultObj<Void> saveExam(@RequestBody SaveExamRequest request) {
        try {
            Vedio video = vedioService.selVedioById(request.getVideoId());
            if (video == null) {
                return ResultObj.error(404, "视频不存在");
            }

            // 将examData转换为JSON字符串
            ObjectMapper objectMapper = new ObjectMapper();
            String examDataJson = objectMapper.writeValueAsString(request.getExamData());

            // 设置试卷内容
            video.setTest(examDataJson);
            vedioService.saveExamToVideo(request.getVideoId(),examDataJson);

            return ResultObj.ok();
        } catch (Exception e) {
            return ResultObj.error(500, "保存试卷失败: " + e.getMessage());
        }
    }





}

